package client.core.io.conection;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;

import proprietes.ConnectProprietes;

import client.ui.workform.DialogForm;

public class ClientToClient extends Conection {

	protected DialogForm dialogTextArea = null;

	public ClientToClient() {

	}

	public ClientToClient(Socket clientSocket){
		this.clientSocket = clientSocket;
	}
	
	public String createConection(String ip) {
		try {
			clientSocket = new Socket(ip,
					ConnectProprietes.CLIENT_PORT.getiValue());
		} catch (UnknownHostException e) {
			return "null";
		} catch (IOException e) {
			return "null";
		}
		return "ok";

	}

	@Override
	public void run() {
		try {
			inputStream = new BufferedReader(new InputStreamReader(
					clientSocket.getInputStream()));
			outputStream = new PrintStream(clientSocket.getOutputStream());
			dialogTextArea = new DialogForm();
			dialogTextArea.setVisible(true);
			dialogTextArea.setStream(outputStream);
			new Thread(new ReadInputStream(inputStream, dialogTextArea))
					.start();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void setDialogForm() {
		this.dialogTextArea = new DialogForm();
		this.dialogTextArea.setVisible(true);
	}
}
